package com.yiren.dbaa.module.project_edit.view.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.enums.VoicePeopleEnum;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.bean.SynExamBean;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.project_edit.view.adapter.SelectParamAdapter;
import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.dbaa.util.DensityUtil;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lwb
 * on 2021/12/29
 * Describe  语音设置Fragment
 */
public class VoiceSetFragment extends Fragment implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    @BindView(R.id.tv_light_voice)
    TextView tvLightVoice;
    @BindView(R.id.sb_voice_speed_light)
    SeekBar sbVoiceSpeedLight;
    @BindView(R.id.tv_voice_speed_light)
    TextView tvVoiceSpeedLight;
    @BindView(R.id.sb_voice_tone_light)
    SeekBar sbVoiceToneLight;
    @BindView(R.id.tv_voice_tone_light)
    TextView tvVoiceToneLight;
    @BindView(R.id.sb_voice_volume_light)
    SeekBar sbVoiceVolumeLight;
    @BindView(R.id.tv_voice_volume_light)
    TextView tvVoiceVolumeLight;

    @BindView(R.id.tv_project_voice)
    TextView tvProjectVoice;
    @BindView(R.id.sb_voice_speed)
    SeekBar sbVoiceSpeed;
    @BindView(R.id.tv_voice_speed)
    TextView tvVoiceSpeed;
    @BindView(R.id.sb_voice_tone)
    SeekBar sbVoiceTone;
    @BindView(R.id.tv_voice_tone)
    TextView tvVoiceTone;
    @BindView(R.id.sb_voice_volume)
    SeekBar sbVoiceVolume;
    @BindView(R.id.tv_voice_volume)
    TextView tvVoiceVolume;

    @BindView(R.id.tv_points_voice)
    TextView tvPointsVoice;
    @BindView(R.id.sb_points_speed)
    SeekBar sbPointsSpeed;
    @BindView(R.id.tv_points_speed)
    TextView tvPointsSpeed;
    @BindView(R.id.sb_points_tone)
    SeekBar sbPointsTone;
    @BindView(R.id.tv_points_tone)
    TextView tvPointsTone;
    @BindView(R.id.sb_points_volume)
    SeekBar sbPointsVolume;
    @BindView(R.id.tv_points_volume)
    TextView tvPointsVolume;

    /*** 初始值 */
    private VoicePeopleEnum lightVoicer;
    private int lightSpeed;
    private int lightTone;
    private int lightVolume;
    private VoicePeopleEnum projectVoicer;
    private int projectSpeed;
    private int projectTone;
    private int projectVolume;
    private VoicePeopleEnum gradeVoicer;
    private int gradeSpeed;
    private int gradeTone;
    private int gradeVolume;

    private List<String> selectList;
    private String selectString;

    private SiteEntity site;
    /*** 存放参数值改变的临时map */
    private Map<String, Object> tempMap;
    private boolean isPartner;
    private SynExamBean synExamBean;
    private Gson gs = new Gson();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_voice_set, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initListener();
        setData();
    }

    private void initData() {
        selectList = new ArrayList<String>();
        site = (SiteEntity) getArguments().getSerializable("site");
        tempMap = new HashMap<>();
        isPartner = LoadConfigApi.loadUserParamConfig(getActivity()).isPartner();
        synExamBean = gs.fromJson(isPartner ? site.getPartneModifyVal() : site.getCoachModifyVal(), new TypeToken<SynExamBean>() {
        }.getType());
        if (null == synExamBean) synExamBean = new SynExamBean();
    }

    private void setData() {
        lightVoicer = VoicePeopleEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y1.toString()));
        tvLightVoice.setText(lightVoicer.getDescription());
        lightSpeed = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y2.toString());
        tvVoiceSpeedLight.setText(lightSpeed + "");
        sbVoiceSpeedLight.setProgress(lightSpeed);
        lightTone = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y3.toString());
        tvVoiceToneLight.setText(lightTone + "");
        sbVoiceToneLight.setProgress(lightTone);
        lightVolume = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y4.toString());
        tvVoiceVolumeLight.setText(lightVolume + "");
        sbVoiceVolumeLight.setProgress(lightVolume);

        projectVoicer = VoicePeopleEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y5.toString()));
        tvProjectVoice.setText(projectVoicer.getDescription());
        projectSpeed = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y6.toString());
        tvVoiceSpeed.setText(projectSpeed + "");
        sbVoiceSpeed.setProgress(projectSpeed);
        projectTone = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y7.toString());
        tvVoiceTone.setText(projectTone + "");
        sbVoiceTone.setProgress(projectTone);
        projectVolume = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y8.toString());
        tvVoiceVolume.setText(projectVolume + "");
        sbVoiceVolume.setProgress(projectVolume);

        gradeVoicer = VoicePeopleEnum.valueOf((String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y9.toString()));
        tvPointsVoice.setText(gradeVoicer.getDescription());
        gradeSpeed = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y10.toString());
        tvPointsSpeed.setText(gradeSpeed + "");
        sbPointsSpeed.setProgress(gradeSpeed);
        gradeTone = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y11.toString());
        tvPointsTone.setText(gradeTone + "");
        sbPointsTone.setProgress(gradeTone);
        gradeVolume = (int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.Y12.toString());
        tvPointsVolume.setText(gradeVolume + "");
        sbPointsVolume.setProgress(gradeVolume);
    }

    private void initListener() {
        sbVoiceSpeedLight.setOnSeekBarChangeListener(this);
        sbVoiceToneLight.setOnSeekBarChangeListener(this);
        sbVoiceVolumeLight.setOnSeekBarChangeListener(this);

        sbVoiceSpeed.setOnSeekBarChangeListener(this);
        sbVoiceTone.setOnSeekBarChangeListener(this);
        sbVoiceVolume.setOnSeekBarChangeListener(this);

        sbPointsSpeed.setOnSeekBarChangeListener(this);
        sbPointsTone.setOnSeekBarChangeListener(this);
        sbPointsVolume.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.sb_voice_speed_light:
                tvVoiceSpeedLight.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y2.toString(), progress);
                break;
            case R.id.sb_voice_tone_light:
                tvVoiceToneLight.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y3.toString(), progress);
                break;
            case R.id.sb_voice_volume_light:
                tvVoiceVolumeLight.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y4.toString(), progress);
                break;
            case R.id.sb_voice_speed:
                tvVoiceSpeed.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y6.toString(), progress);
                break;
            case R.id.sb_voice_tone:
                tvVoiceTone.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y7.toString(), progress);
                break;
            case R.id.sb_voice_volume:
                tvVoiceVolume.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y8.toString(), progress);
                break;
            case R.id.sb_points_speed:
                tvPointsSpeed.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y10.toString(), progress);
                break;
            case R.id.sb_points_tone:
                tvPointsTone.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y11.toString(), progress);
                break;
            case R.id.sb_points_volume:
                tvPointsVolume.setText(progress + "");
                SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y12.toString(), progress);
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @OnClick({R.id.tv_light_voice, R.id.tv_project_voice, R.id.tv_points_voice, R.id.tv_save_param})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_light_voice:
                setDialogValues(1, tvLightVoice);
                break;
            case R.id.tv_project_voice:
                setDialogValues(2, tvProjectVoice);
                break;
            case R.id.tv_points_voice:
                setDialogValues(3, tvPointsVoice);
                break;
            case R.id.tv_save_param:
                if (sbVoiceSpeedLight.getProgress() != lightSpeed) {
                    tempMap.put(SiteConfigCodeEnum.Y2.toString(), sbVoiceSpeedLight.getProgress());
                }
                if (sbVoiceToneLight.getProgress() != lightTone) {
                    tempMap.put(SiteConfigCodeEnum.Y3.toString(), sbVoiceToneLight.getProgress());
                }
                if (sbVoiceVolumeLight.getProgress() != lightVolume) {
                    tempMap.put(SiteConfigCodeEnum.Y4.toString(), sbVoiceVolumeLight.getProgress());
                }
                if (sbVoiceSpeed.getProgress() != projectSpeed) {
                    tempMap.put(SiteConfigCodeEnum.Y6.toString(), sbVoiceSpeed.getProgress());
                }
                if (sbVoiceTone.getProgress() != projectTone) {
                    tempMap.put(SiteConfigCodeEnum.Y7.toString(), sbVoiceTone.getProgress());
                }
                if (sbVoiceVolume.getProgress() != projectVolume) {
                    tempMap.put(SiteConfigCodeEnum.Y8.toString(), sbVoiceVolume.getProgress());
                }
                if (sbPointsSpeed.getProgress() != gradeSpeed) {
                    tempMap.put(SiteConfigCodeEnum.Y10.toString(), sbPointsSpeed.getProgress());
                }
                if (sbPointsTone.getProgress() != gradeTone) {
                    tempMap.put(SiteConfigCodeEnum.Y11.toString(), sbPointsTone.getProgress());
                }
                if (sbPointsVolume.getProgress() != gradeVolume) {
                    tempMap.put(SiteConfigCodeEnum.Y12.toString(), sbPointsVolume.getProgress());
                }
                if (tempMap.size() == 0) return;
                for (String key : tempMap.keySet()) {
                    synExamBean.getExamConfigMap().put(key, tempMap.get(key));
                }
                if (isPartner) {
                    site.setPartneModifyVal(gs.toJson(synExamBean));
                } else {
                    site.setCoachModifyVal(gs.toJson(synExamBean));
                }
                UserDbController.getInstance(false).update(site);
                MessageBox.show(getActivity(), "保存成功!");
                break;
        }
    }

    /**
     * 设置对话框的值
     */
    private void setDialogValues(int type, TextView tv) {
        selectList.clear();
        selectString = tv.getText().toString();
        VoicePeopleEnum[] voicePeoples = VoicePeopleEnum.values();
        for (int i = 0; i < voicePeoples.length; i++) {
            selectList.add(voicePeoples[i].getDescription());
        }
        showSelectDialog(type, tv);
    }

    /**
     * 弹出框选择（type 1:灯光  2：标准项目语音  3:扣分语音）
     */
    public void showSelectDialog(int type, TextView tv) {
        Dialog dialog = new AlertDialog.Builder(getActivity()).create();
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (DensityUtil.getScreenWidth(getContext()) * 7) / 10;
        params.height = (DensityUtil.getScreenHeight(getContext()) * 6) / 10;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_box);
        TextView tvTitle = (TextView) window.findViewById(R.id.tv_title);
        ListView lvList = (ListView) window.findViewById(R.id.lv_dialog_list);
        lvList.setDivider(null);// 去除listview的下划线
        tvTitle.setText(getResources().getString(R.string.param_voice_people_tip));
        SelectParamAdapter adapter = new SelectParamAdapter(getActivity(), selectList, selectString);
        lvList.setAdapter(adapter);
        lvList.setOnItemClickListener((parent, view, position, id) -> {
            tv.setText(selectList.get(position));
            VoicePeopleEnum[] voicePeoples = VoicePeopleEnum.values();
            switch (type) {
                case 1:
                    if (!lightVoicer.equals(voicePeoples[position])) {
                        SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y1.toString(), voicePeoples[position].toString());
                        tempMap.put(SiteConfigCodeEnum.Y1.toString(), voicePeoples[position].toString());
                    } else {
                        tempMap.remove(SiteConfigCodeEnum.Y1.toString());
                    }
                    break;
                case 2:
                    if (!projectVoicer.equals(voicePeoples[position])) {
                        SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y5.toString(), voicePeoples[position].toString());
                        tempMap.put(SiteConfigCodeEnum.Y5.toString(), voicePeoples[position].toString());
                    } else {
                        tempMap.remove(SiteConfigCodeEnum.Y5.toString());
                    }
                    break;
                case 3:
                    if (!gradeVoicer.equals(voicePeoples[position])) {
                        SignalGConstant.examConfigMap.put(SiteConfigCodeEnum.Y9.toString(), voicePeoples[position].toString());
                        tempMap.put(SiteConfigCodeEnum.Y9.toString(), voicePeoples[position].toString());
                    } else {
                        tempMap.remove(SiteConfigCodeEnum.Y9.toString());
                    }
                    break;
            }
            dialog.dismiss();
        });
    }

}